package service.edu.controller;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import service.edu.entity.Teacher;
import service.edu.entity.vo.TeacherQuery;
import service.edu.service.TeacherService;
import service.edu.result.R;
import java.util.List;



/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author diao
 * @since 2020-07-29
 */
@Api("讲师管理")
@RestController
@RequestMapping("/edu/teacher")
@CrossOrigin
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    //查询讲师表所有数据
    @ApiOperation("查询所有讲师")
    @GetMapping("/queryAll")
    public R queryAllTeachers(){
        List<Teacher> teacherList = teacherService.list(null);
/*        try {
            int i=1/0;
        } catch (Exception e) {
            log.error(e.getMessage());//将错误输入error日志中
            throw new CustomException(202,"执行了自定义异常处理...");
        }*/
        return R.Ok().data("teacherList",teacherList);
    }

    //根据id逻辑删除讲师数据
    @ApiOperation("根据id逻辑删除讲师")
    @DeleteMapping("/delete/{id}")
   public R deleteById(@PathVariable("id")String id){
        boolean b = teacherService.removeById(id);
        if (b)
          return R.Ok();
        else
            return R.Error();
    }

    //根据id查询讲师
    @ApiOperation("根据id查询讲师")
    @GetMapping("/query/{id}")
    public R queryById(@ApiParam(name = "id",value = "讲师id")@PathVariable("id")String id){
        Teacher teacher = teacherService.getById(id);
        return R.Ok().data("teacher", teacher);
    }


    /**分页查询讲师
     * current:当前页
     * limit:每页记录数
     */
    @ApiOperation("分页查询讲师")
    @GetMapping("/query/page/{current}/{limit}")
    public R queryByPage(@ApiParam(name = "current", value = "当前页码", required = true)
                         @PathVariable("current")long current,
                         @ApiParam(name = "limit", value = "每页记录数", required = true)
                         @PathVariable("limit")long limit){
        Page<Teacher> page = new Page<>(current,limit);
        teacherService.page(page, null);//分页查询
        long total=page.getTotal();//总记录数
        List<Teacher> records = page.getRecords();//数据list集合
        return R.Ok().data("total", total).data("rows",records);
    }

    /**分页条件查询
     * current:当前页
     * limit:每页记录数
     * TeacherQuery:教师查询条件封装类
     * 加了requestBody的参数需要以json字符串的形式传递数据
     */
    @ApiOperation("分页条件查询讲师")
    @PostMapping("/query/page/condition/{current}/{limit}")
    public R queryByConditionAndPage(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable("current")long current,
                                     @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit")long limit,
                                     @ApiParam(name = "teacherQuery",value = "查询的条件对象") @RequestBody(required = false) TeacherQuery teacherQuery)
    {
        Page<Teacher> page = new Page<>(current,limit);
        //进行条件查询
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        String name = teacherQuery.getName();//教师名称
        Integer level = teacherQuery.getLevel();//头衔:1高级讲师 2首席讲师
        String begin = teacherQuery.getBegin();//查询开始时间
        String end = teacherQuery.getEnd();//查询结束时间
        //条件查询是根据数据库字段来鉴别的
        if (!StringUtils.isEmpty(name))
          queryWrapper.like("name", name);
        if (!StringUtils.isEmpty(level))
          queryWrapper.eq("level", level);
        if (!StringUtils.isEmpty(begin))
          queryWrapper.ge("gmt_create", begin);
        if (!StringUtils.isEmpty(end))
          queryWrapper.le("gmt_create", end);
        //排序
        queryWrapper.orderByAsc("gmt_create");
        //开始分页查询
        teacherService.page(page, queryWrapper);
        //先进行条件查询后再返回分页查询的结果
        long total = page.getTotal();//总记录数
        List<Teacher> records = page.getRecords();//数据list集合
        //返回数据
        return R.Ok().data("total", total).data("rows",records);
    }

    //增加讲师
    @ApiOperation("添加教师")
    @PostMapping("/addTeacher")
    public R addTeacher(@ApiParam(name = "teacher",value = "添加的讲师")@RequestBody(required = false)Teacher teacher){
        boolean save = teacherService.save(teacher);
        if (save)
            return R.Ok();
        else
            return R.Error();
    }

    //更改讲师信息,讲师对象id必填
    @ApiOperation("根据id更改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@ApiParam(name = "teacher",value = "新的讲师")@RequestBody(required = false) Teacher teacher){
        boolean update = teacherService.updateById(teacher);
        if (update)
            return R.Ok();
        else
            return R.Error();
    }


}

